<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="服务商"
        >
          <a-select :disabled="isDisabled"
                    showSearch
                    optionFilterProp="children"
                    :filterOption="filterOption"
                    v-decorator="[ 'parentId', {rules: [{ required: true, message: '请选择。。。' }]} ]">
            <a-select-option v-for="item in businessList" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="登录账户"
        >
          <a-input placeholder="登录账户" v-decorator="[ 'username', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="密码"
        >
          <a-input placeholder="密码" type="password" v-decorator="[ 'password', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-radio-group v-decorator="[ 'status', {rules: [{ required: true, message: '请选择' }]}]">
            <a-radio :value="1">禁用</a-radio>
            <a-radio :value="0">启用</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-divider/>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="拥有角色"
        >
          <a-select mode="multiple"
                    v-decorator="[ 'roleIds', {rules: [{ required: true, message: '请选择' }]} ]">
            <a-select-option v-for="item in roles" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { selectAllRole, selectRoleIdByUserId } from '@/api/system/role'
  import { save } from '@/api/system/user'
  import { selectAll } from '@/api/businessService/business'
  import pick from 'lodash.pick'

  export default {
    //--############  data() ###############&ndash;&gt;
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),

        isDisabled: false,
        businessList: [],
        roles: []
      }
    },
    //--############  data() ###############&ndash;&gt;

    created() {
      selectAll().then(res => {
        console.log("selectAll",res.result)
        this.businessList = res.result
      })
      selectAllRole(1).then(res => {
        this.roles = res.result
      })
    },

    //--############  methods() ###############&ndash;&gt;
    methods: {
      add() {
        this.isDisabled = false
        this.visible = true
      },
      filterOption(input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },
      edit(record) {
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true
        if(record.status){
          this.mdl.status = 1
        }else{
          this.mdl.status = 0
        }
        selectRoleIdByUserId(record.id).then(res => {
          this.mdl.roleIds = res.result
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.mdl, 'parentId','username', 'password','status', 'roleIds'))
          })
        })

      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl={}
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id;
            _this.confirmLoading = true
            // 模拟后端请求 2000 毫秒延迟
            values.type=1
            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              }else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
